package com.vx.vxbug.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vx.vxbug.entities.Comment;

@Repository("commentDao")
public class SqlCommentDao implements ICommentDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional(propagation = Propagation.REQUIRED)
	public void addComment(Comment b) {
		entityManager.persist(b);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Comment getCommentById(int id) {
		return entityManager.find(Comment.class, id);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Comment> getAllCommentsForBugId(int bugId) {
		List<Comment> result = entityManager.createQuery("SELECT i FROM Comment i WHERE i.bugId LIKE :bugId").setParameter("bugId", bugId).getResultList();
		return result;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void updateComment(Comment b) {
		entityManager.refresh(b);
	}

	public SqlCommentDao() {
		super();
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteComment(Comment b) {
		entityManager.remove(b);
	}

}
